package www.lagou.app;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import www.lagou.entity.Phone;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

public class PhoneTest {
    /**
     * 需求1:  查询价格高于2000元，生产日期是2019年之前的所有手机
     */
    public static void queryTaskOne() {
        QueryRunner qr = new QueryRunner(DruidUtils.DATASOURCE);
        String sql = "select * from phone where price>2000 and prodate<'2019-01-01'";
        try {
            List<Phone> phoneList = qr.query(sql, new BeanListHandler<Phone>(Phone.class));
            System.out.println("价格高于2000元，生产日期是2019年之前的手机有：");
            for (Phone phone : phoneList) {
                System.out.println(phone);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 需求2:  查询所有颜色是白色的手机信息
     */
    public static void queryTaskTwo() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.DATASOURCE);
        String sql = "select * from phone where color=?";
        List<Phone> phoneList = null;
        try {
            phoneList = queryRunner.query(sql, new BeanListHandler<Phone>(Phone.class), "白色");
            System.out.println("白色的手机有：");
            for (Phone phone : phoneList) {
                System.out.println(phone);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        queryTaskOne();
        System.out.println("---------------------------------------------");
        queryTaskTwo();
    }
}
